﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffDesktop
{
    /*
     * Class: Caution
     * 
     * Use: Form used to model and edit details for a Caution
     *      within a Match
     *      
     * Author: Mike Skaife, mcs3@aber.ac.uk
     */ 
    public partial class Caution : Form
    {
        //The Match that the Caution belogs to
        public Match referrer;

        //Details required for connecting to the database
        SqlCeConnection conn;
        String conString;
        String desktop_connection = @"Data Source=RefOff_DB.sdf";
        
        //Variables to hold details of the Caution
        int cCaution_id;
        String cCautionCode;
        String cC1Code;
        int cPlayer_id;
        String cPlayerName;
        String cPlayerSurname;
        String cPlayerFirstname;
        String cClub;
        int cMatch_id;
        String cTime;
        String cWhistleGPS = "";
        String cIncidentGPS = "";

        //Array of all players in the match the caution belongs to
        public List<Player> cautionMatchPlayers;

        //Array of all possible disciplinary codes for use in combo boxes
        public List<String> cautionCodes;
        public List<String> c1Codes;
        
        public Caution()
        {
            InitializeComponent();
            referrer = null;
            cautionMatchPlayers = new List<Player>();
            
            //Add disciplinary codes to combo boxes
            cautionCodes = new List<String>();
            cautionCodes.Add("C1");
            cautionCodes.Add("C2");
            cautionCodes.Add("C3");
            cautionCodes.Add("C4");
            cautionCodes.Add("C5");
            cautionCodes.Add("C6");
            cautionCodes.Add("C7");
            
            //Add C1 disciplinary codes to combo boxes
            c1Codes = new List<String>();
            c1Codes.Add("-");
            c1Codes.Add("AA");
            c1Codes.Add("DI");
            c1Codes.Add("DP");
            c1Codes.Add("FT");
            c1Codes.Add("GC");
            c1Codes.Add("HB");
            c1Codes.Add("RP");
            c1Codes.Add("SP");
            c1Codes.Add("TR");
            c1Codes.Add("UB");

            //Initialise combo box values on form
            addCautionCodes();
            addC1Codes();            
        }

        //Method used to add items to the caution code combo box
        private void addCautionCodes()
        {
            //Loop through each of the available codes
            for (int i = 0; i < cautionCodes.Count; i++)
            {
                //Add the code to the list of combo box items
                cautionCautionCodeCombo.Items.Add(cautionCodes[i]);
            }
        }

        //Method used to add items to the C1 code combo box
        private void addC1Codes()
        {
            //Loop through each of the available codes
            for (int i = 0; i < c1Codes.Count; i++)
            {
                //Add the code to the list of combo box items
                cautionC1CodeCombo.Items.Add(c1Codes[i]);
            }
        }

        //Method used to select the relevant combo box item for the disciplinary
        //codes when the form is opened by the user
        public void initCode()
        {
            int index = 0;
            int counter = 0;

            //Loop through all items in the combo box
            for (int i = 0; i < cautionCautionCodeCombo.Items.Count; i++)
            {                
                //If the current item matches the code of the Caution
                if (cautionCautionCodeCombo.Items[i].ToString().Equals(cCautionCode))
                {
                    //Get hold of the index of the item
                    index = counter;
                }
                counter++;
            }

            //And select the item with that index in the combo box
            cautionCautionCodeCombo.SelectedIndex = index;

            //If the code for this caution is 'C1'
            if (cCautionCode.Equals("C1"))
            {
                //Enable the C1 combo box so the relevant C1 code can be chosen
                cautionC1CodeCombo.Enabled = true;
            }
            else
            {
                //Otherwise disable the C1 combo box as it is not needed for other cautions
                cautionC1CodeCombo.Enabled = false;
            }
        }

        //Method used to select the relevant combo box item for the C1
        //codes when the form is opened by the user
        public void initC1Code()
        {
            int index = 0;
            int counter = 0;

            //Loop through each item in the C1 combo box
            for (int i = 0; i < cautionC1CodeCombo.Items.Count; i++)
            {
                //If the current item matches the C1 code for this caution
                if (cautionC1CodeCombo.Items[i].ToString().Equals(cC1Code))
                {
                    //Get hold of the index of the item
                    index = counter;
                }
                counter++;
            }

            //And select the relevant item in the C1 combo box
            cautionC1CodeCombo.SelectedIndex = index;
        }

        //Setter and getter for id of Caution
        public int caution_id
        {
            get
            {
                return cCaution_id;
            }

            set
            {
                cCaution_id = value;
            }
        }

        //Setter and getter for disciplinary code of Caution
        public String caution_code
        {
            get
            {
                return cCautionCode;
            }

            set
            {
                cCautionCode = value;
            }
        }

        //Setter and getter for C1 code of Caution
        public String c1_code
        {
            get
            {
                return cC1Code;
            }

            set
            {
                cC1Code = value;
            }
        }

        //Setter and getter for id of player receiving Caution
        public int player_id
        {
            get
            {
                return cPlayer_id;
            }

            set
            {
                cPlayer_id = value;
            }
        }

        //Setter and getter for name of player receiving Caution
        public String player_name
        {
            get
            {
                return cPlayerName;
            }

            set
            {
                cPlayerName = value;
            }
        }

        //Setter and getter for surname of player receiving Caution
        public String player_surname
        {
            get
            {
                return cPlayerSurname;
            }

            set
            {
                cPlayerSurname = value;
            }
        }

        //Setter and getter for firstname of player receiving Caution
        public String player_firstname
        {
            get
            {
                return cPlayerFirstname;
            }

            set
            {
                cPlayerFirstname = value;
            }
        }

        //Setter and getter for name of club receiving Caution
        public String club
        {
            get
            {
                return cClub;
            }

            set
            {
                cClub = value;
            }
        }

        //Setter and getter for time in match when Caution was given
        public String time
        {
            get
            {
                return cTime;
            }

            set
            {
                cTime = value;
            }
        }

        //Setter and getter for id of match in which Caution was given
        public int match_id
        {
            get
            {
                return cMatch_id;
            }

            set
            {
                cMatch_id = value;
            }
        }

        //Setter and getter for GPS location of whistle event for Caution
        public String whistleGPS
        {
            get
            {
                return cWhistleGPS;
            }

            set
            {
                cWhistleGPS = value;
            }
        }

        //Setter and getter for GPS location of Caution incident
        public String incidentGPS
        {
            get
            {
                return cIncidentGPS;
            }

            set
            {
                cIncidentGPS = value;
            }
        }

        //This method runs when the 'Close' button is clicked
        private void cautionCloseBtn_Click(object sender, EventArgs e)
        {
            //Hide this screen and enable the Match Details screen
            referrer.Enabled = true;
            this.Visible = false;

            //Cancel any selections made by the user in the combo boxes
            initCode();
            initC1Code();
        }

        //This method runs when the user selects a disciplinary code in the first combo box
        private void cautionCautionCodeCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = 0;

            //Get hold of the index of the item that was selected
            index = cautionCautionCodeCombo.SelectedIndex;

            //Determine if the selected code was 'C1'
            if (cautionCautionCodeCombo.Items[index].ToString().Equals("C1"))
            {
                //If so, enable the C1 code combo box so the C1 code can
                //be selected
                cautionC1CodeCombo.Enabled = true;
            }
            else
            {
                //Otherwise disable the C1 combo box
                cautionC1CodeCombo.Enabled = false;
            }
        }

        //This method is run when the 'Save' button is clicked
        private void cautionSaveBtn_Click(object sender, EventArgs e)
        {
            //Get the index of the selected combo box items and their
            //corresponding values
            int index = cautionCautionCodeCombo.SelectedIndex;
            cCautionCode = cautionCautionCodeCombo.Items[index].ToString();
            int c1index = cautionC1CodeCombo.SelectedIndex;
            String tempC1 = cautionC1CodeCombo.Items[c1index].ToString();

            //Connect to the database
            connectToDatabase(desktop_connection);

            //Create and execute SQL command to update details of the Caution in the database
            string caution_update_command = @"UPDATE caution SET caution_code = @code, c1_code = @c1 WHERE caution_id= " + cCaution_id + ";";
            using (SqlCeCommand caution_update = new SqlCeCommand(caution_update_command, conn))
            {
                caution_update.Parameters.AddWithValue("@code", cCautionCode);
                //If caution code selectdd is 'C1'
                if (cCautionCode.Equals("C1"))
                {
                    //Add the value of the C1 combo box as a parameter
                    cC1Code = tempC1;
                    caution_update.Parameters.AddWithValue("@c1", cC1Code);
                }
                else
                {
                    //Otherwise set it to a 'blank' value
                    cC1Code = "-";
                    caution_update.Parameters.AddWithValue("@c1", cC1Code);
                }

                //Execute the SQL command
                caution_update.ExecuteNonQuery();
            }

            //Disconnect from the database
            closeDatabaseConnection();

            //Close this form and display the Match Details screen
            referrer.Enabled = true;
            this.Visible = false;

            //Set the default selected values of the combo boxes to reflect the changes
            initCode();
            initC1Code();

            //Update details of the Caution in the Match Details screen to reflect the changes
            referrer.matchCautionLst.SelectedItems[0].SubItems[2].Text = cCautionCode;
            referrer.matchCautionLst.SelectedItems[0].SubItems[3].Text = cC1Code;
        }

        //Method used to connect to the database
        private void connectToDatabase(String connection_string)
        {
            // Retrieve the connection string
            conString = connection_string;

            // Open the connection using the connection string
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //Method used to disconnect from the database
        private void closeDatabaseConnection()
        {
            //Close the database connection
            conn.Close();
        }
    }
}
